package de.baumann.browser.activitys.oc

import android.app.Activity
import android.content.Intent
import cn.bingoogolapple.qrcode.core.QRCodeView
import cn.bingoogolapple.qrcode.zxing.ZXingView
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.utils.ViewUtil
import kotlinx.android.synthetic.main.activity_scan.*

class ScanActivity : BaseOdinActivity<IBaseView,BasePresenter<IBaseView>>(), QRCodeView.Delegate {
    override fun getLayout(): Int {
        return R.layout.activity_scan
    }

    override fun initView() {
        zxingview.setDelegate(this)
    }

    override fun initData() {}
    override fun onResume() {
        super.onResume()
        zxingview!!.startCamera()
        zxingview!!.startSpotAndShowRect()
    }

    override fun onPause() {
        super.onPause()
        zxingview!!.stopCamera()
    }

    override fun onDestroy() {
        super.onDestroy()
        zxingview!!.onDestroy()
    }

    override fun createPresenter(): BasePresenter<IBaseView>? {
        return null
    }

    override fun createView(): IBaseView? {
        return null
    }

    override fun onScanQRCodeSuccess(result: String) {
        if (result == null) {
            return
        }
        ViewUtil.scanPlay(mContext)
        if (result.startsWith("0x") || result.startsWith("odx")) {
            val intent = intent
            intent.putExtra("toAddress", result)
            setResult(Activity.RESULT_OK, intent)
            finish()
        } else {
            showShortToast("内容无效，请确认二维码")
        }
    }

    override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {}
    override fun onScanQRCodeOpenCameraError() {
        showShortToast("相机打开失败")
    }

    companion object {
        fun startScanActivity(context: Activity, requestCode: Int) {
            val intent = Intent(context, ScanActivity::class.java)
            context.startActivityForResult(intent, requestCode)
        }
    }
}